<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">
        退出
      </el-button>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 菜单展开折叠按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#16a085"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu v-for="item in menulist" :key="item.id" :index="item.id.toString()">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="subitem in item.children"
              :key="subitem.id"
              :index="'/' + subitem.path.toString()"
              @click="saveNavState('/' + subitem.path.toString())"
            >
              <!-- 二级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主要内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    data() {
      return {
        menulist: [],
        iconsObj: {
          125: 'el-icon-s-custom',
          103: 'el-icon-s-check',
          101: 'el-icon-s-goods',
          102: 'el-icon-s-order',
          145: 'el-icon-s-data'
        },
        isCollapse: false, // 用于控制菜单的折叠与展开,默认展开
        activePath: '' // 被激活的链接地址
      }
    },
    // 定义一个生命周期函数,此时 vm 已经创建完毕,还未进行挂载
    created() {
      // 获取菜单列表
      this.getMenuList()
      //  获取被激活的链接地址
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      logout() {
        // 退出登录
        window.sessionStorage.clear()
        this.$router.push('/login')
      },
      // 获取所有的菜单
      async getMenuList() {
        const { data: res } = await this.$http.get('menus')
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.menulist = res.data
        //   console.log(res)
      },
      toggleCollapse() {
        // 点击切换菜单的折叠与展开
        this.isCollapse = !this.isCollapse
      },
      saveNavState(activePath) {
        // 保存链接的激活状态
        window.sessionStorage.setItem('activepath', activePath)
        this.activePath = activePath
      }
    }
  }
</script>

<style lang="less" scoped>
  .home-container {
    width: 100%;
    height: 100%;
  }

  .el-header {
    display: flex;
    justify-content: space-between;
    align-content: center;
    padding-left: 0;
    background-color: #373d41;
    color: #fff;
    font-size: 20px;
    div {
      display: flex;
      align-items: center;
      margin-left: 5px;
      img {
        height: 100%;
      }

      span {
        margin-left: 15px;
      }
    }

    .el-button {
      height: 70%;
      transform: translateY(22%);
    }
  }

  .el-aside {
    background-color: #333744;
    .el-menu {
      //  菜单右侧边框线对不齐,是因为 el-menu 有一个 border-right 样式
      border-right: 0;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }

  .toggle-button {
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    background-color: #475163;
    // 给折叠提示的三个竖线(|||)之间隔开一点距离
    letter-spacing: 0.2rem;
    cursor: pointer;
    user-select: none;
  }
</style>
